<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    :rules="rules"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:" prop="username">
      <el-input v-model="userInfo.username" style="width:300px" />
    </el-form-item>
    <el-form-item label="密码:" prop="password">
      <el-input v-model="userInfo.password" disabled type="password" style="width:300px" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="updateUserDetailById">更新</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'

export default {
  name: 'LoginSetting',
  // 导入组件名
  components: {},
  // 父传子,接收变量名
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    // 这里存放数据
    return {
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  // 计算属性 类似于 data 概念
  computed: {},
  // 监听器 监控 data 中的数据变化
  watch: {},
  created() {
    this.loadUserDetailById()
  },
  // 方法集合
  methods: {
    // 获取员工信息
    async loadUserDetailById() {
      try {
        const res = await getUserDetailById(this.id)
        console.log('getUserDetailById', res)
        this.userInfo = res.data
      } catch (err) {
        console.log('getUserDetailById', err)
      }
    },
    // 修改员工信息
    async updateUserDetailById() {
      try {
        const res = await saveUserDetailById(this.userInfo)
        console.log('saveUserDetailById', res)
        // 跳回原来的页面
        this.$router.back()
      } catch (err) {
        console.log('saveUserDetailById', err)
      }
    }

  }
}
</script>

<style scoped>
</style>
